package gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import client.ClientConsole;

import logic.BookItem;
import logic.Librarian;
import logic.Message;
import logic.Title;


/**
 * Creates inventory management window for books
 * enables editing, deleting and adding books 
 */
public class InventoryManagmentWindow extends JPanel implements ActionListener{
	private JPanel mainPanel,searchPanel,bookFormPanel,bottomPanel,buttonPanel,buttonBottom,bottomAdd;
	private JTextField bookSearch,name,author,edition,printDate,subject,purchaseDate,location,copies;
	private JLabel searchLabel,nameLabel,authorLabel,editionLabel,printDateLabel,
	subjectLabel,purchaseDateLabel,locationLabel,descriptionLabel,error,wanted,copiesLabel;
	private JButton search,edit,delete,back,add;
	/** if selected the book is wanted, otherwise not wanted*/
	private JCheckBox wantedCheckBox;
	/** description of the book*/
	private JTextPane description;
	GridBagConstraints gbc = new GridBagConstraints();
	/** holds the reference of the book being chosen*/
	Title title=new Title();
	
	/**
	 * Default constructor-Builds inventory management window
	 */
	public InventoryManagmentWindow()
	{
		super();
		mainPanel=new JPanel();
		mainPanel.setLayout(new BorderLayout());
		bookSearch=new JTextField(20);
		searchLabel=new JLabel("Catalog number: ");
		search= new JButton("Search");
		search.addActionListener(this);
		searchPanel=new JPanel();
		searchPanel.add(searchLabel);
		searchPanel.add(bookSearch);
		searchPanel.add(search);
		mainPanel.add(searchPanel,BorderLayout.NORTH);
		bookFormPanel=new JPanel();
		bookFormPanel.setLayout(new GridBagLayout());
		nameLabel=new JLabel("Name: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(nameLabel,gbc);
		name=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=0;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(name,gbc);
		authorLabel=new JLabel("Author: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=1;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(authorLabel,gbc);
		author=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.insets= new Insets(5,5,5,5);
		gbc.insets= new Insets(7,7,7,7);
		bookFormPanel.add(author,gbc);
		printDateLabel=new JLabel("Print Date: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=4;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(printDateLabel,gbc);
		printDate=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=4;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(printDate,gbc);
		editionLabel=new JLabel("Edition: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=3;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(editionLabel,gbc);
		edition=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=3;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(edition,gbc);
		subjectLabel=new JLabel("Subject: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=2;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(subjectLabel,gbc);
		subject=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=2;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(subject,gbc);
		purchaseDateLabel=new JLabel("Purchase Date: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=5;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(purchaseDateLabel,gbc);
		purchaseDate=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=5;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(purchaseDate,gbc);
		locationLabel=new JLabel("Location: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=6;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(locationLabel,gbc);
		location=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=6;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(location,gbc);
		copiesLabel=new JLabel("Number Of Copies: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=7;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(copiesLabel,gbc);
		copies=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=7;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(copies,gbc);
		wanted=new JLabel("Wanted: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=8;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(wanted,gbc);
		wantedCheckBox=new JCheckBox();
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=8;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(wantedCheckBox,gbc);
		descriptionLabel=new JLabel("Description: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=9;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(descriptionLabel,gbc);
		description=new JTextPane();
		description.setSize(50, 50);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=9;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(description,gbc);
		buttonPanel= new JPanel();
		edit=new JButton("Edit");
		delete=new JButton("Delete");
		buttonPanel.add(edit);
		buttonPanel.add(delete);
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=10;
		gbc.gridwidth=2;
		gbc.insets= new Insets(5,5,5,5);
		bookFormPanel.add(buttonPanel,gbc);
		error=new JLabel();
		edit.addActionListener(this);
		delete.addActionListener(this);
		mainPanel.add(bookFormPanel,BorderLayout.CENTER);
		bookFormPanel.setVisible(false);
		bottomPanel=new JPanel(new BorderLayout());
		add=new JButton();
		add.setIcon(new ImageIcon("C:\\images\\addbook.png"));
		bottomAdd=new JPanel();
		bottomAdd.add(add);
		bottomPanel.add(bottomAdd,BorderLayout.CENTER);
		back=new JButton("back");
		back.addActionListener(this);
		buttonBottom=new JPanel();
		buttonBottom.add(back);
		buttonBottom.add(error);
		bottomPanel.add(buttonBottom,BorderLayout.SOUTH);
		mainPanel.add(bottomPanel,BorderLayout.SOUTH);
		add.addActionListener(this);
		add(mainPanel);
	}
	//Handles user's clicks on buttons
	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource()==back)
		{
			
			this.setVisible(false);
			if (ClientConsole.reader instanceof LibraryManager)
				ClientConsole.aFrame.setContentPane(new LibraryManagerWindow());
			else if (ClientConsole.reader instanceof Librarian)
				ClientConsole.aFrame.setContentPane(new LibrarianWindow());
		}
		if (ae.getSource()==search)
		{
			if (bookSearch.getText().isEmpty())
			{
				error.setText("Please fill search field");
				error.setVisible(true);
				bookFormPanel.setVisible(false);
			}
			else	//search for a book and show its properties
			{
				Message m = new Message("Get Book",new BookItem(Integer.parseInt(bookSearch.getText())));
				ClientConsole.HandleMessageFromGui(m);
				m=(Message)ClientConsole.client.getMessage();
				if(m.getCommand().equals("Book found")){
					String pattern="dd/MM/yyyy";
					SimpleDateFormat dateFormat=new SimpleDateFormat(pattern);
					bookFormPanel.setVisible(true);
					title=(Title) m.getObjects();
					name.setText(title.getName());
					subject.setText(title.getSubject());
					edition.setText(title.getEditionNum());				
					printDate.setText(dateFormat.format(title.getPrintDate()));
					purchaseDate.setText(dateFormat.format(title.getPurchaseDate()));
					author.setText(title.getAuthor());
					location.setText(title.getShelfLocation());
					description.setText(title.getDescription());
					copies.setText(String.valueOf(title.getCopies()));
					
					if (title.getWanted()==true)
						wantedCheckBox.setSelected(true);
					else
						wantedCheckBox.setSelected(false);
					error.setVisible(false);
				}
				else
				{
					error.setText(m.getCommand());
					bookFormPanel.setVisible(false);
				}
			}
		}
		//edit a book
		if (ae.getSource()==delete)
		{
			Message m=new Message("Delete Book",new BookItem(title.getCatalogNumber()));
			ClientConsole.HandleMessageFromGui(m);
			m=(Message)ClientConsole.client.getMessage();
			
			if(m.getCommand().equals("Book Deleted"))
			{
				error.setText("Deleted successfully");
				error.setVisible(true);
				bookFormPanel.setVisible(false);
			}
			else
			{
				error.setText(m.getCommand());
				error.setVisible(true);
			}
		}
		if (ae.getSource()==edit)	//edit a book
		{
			if (author.getText().isEmpty() || name.getText().isEmpty()|| description.getText().isEmpty() || subject.getText().isEmpty()|| location.getText().isEmpty()
					|| edition.getText().isEmpty()|| printDate.getText().isEmpty() || purchaseDate.getText().isEmpty()|| copies.getText().isEmpty())
			{
				error.setVisible(true);
				error.setText("Please Fill all fields");
			}
			else
			{
				String pattern="dd/MM/yyyy";
				SimpleDateFormat dateFormat=new SimpleDateFormat(pattern);
				Calendar cal;
				Title editedTitle=new Title();
				editedTitle.setCatalogNumber(title.getCatalogNumber());
				editedTitle.setAuthor(author.getText());
				editedTitle.setName(name.getText());
				editedTitle.setDescription(description.getText());
				editedTitle.setSubject(subject.getText());
				editedTitle.setShelfLocation(location.getText());
				editedTitle.setEditionNum(edition.getText());
				editedTitle.setCopies(Integer.parseInt(copies.getText()));
				try {
					editedTitle.setPrintDate(dateFormat.parse(printDate.getText()));
					cal=dateFormat.getCalendar();
					System.out.println("time in mili: "+cal.getTimeInMillis());
					System.out.println(dateFormat.parse(printDate.getText()));
					editedTitle.setPurchaseDate(dateFormat.parse(purchaseDate.getText()));
				} catch (ParseException e) {
					System.out.printf("Error parsing date");
				}
				if (wantedCheckBox.isSelected()==true)
					editedTitle.setWanted(true);
				else
					editedTitle.setWanted(false);
				
				Message m=new Message("Edit Book",editedTitle);
				ClientConsole.HandleMessageFromGui(m);
				m=(Message)ClientConsole.client.getMessage();
				if(m.getCommand().equals("Book edited"))
				{
					error.setText("Deleted successfully");
					error.setVisible(true);
				}
				else
				{
					error.setText(m.getCommand());
					error.setVisible(true);
					bookFormPanel.setVisible(false);
				}
			}
		}
		//go to AddBook form
		else if (ae.getSource()==add)
		{
			AddBookForm abf=new AddBookForm();
			ClientConsole.aFrame.setContentPane(abf);
			this.setVisible(false);
		}
		
	}
}
